import { getPermissionListAPI, deletePermissionAPI, addPermissionAPI, findPermissionAPI, updatePermissionAPI } from '@/api/permissionsApi'
import { transToTreeData } from '@/utils'

const actions = {
  /* 发送请求获取权限列表的action */
  async fetchPermissionList({ commit }) {
    const { data } = await getPermissionListAPI()
    const permissionList = transToTreeData(data, '0')
    commit('updatePermissionList', permissionList)
    return permissionList
  },
  /* 发送请求删除权限的action */
  async deletePermission({ commit }, id) {
    const { message } = await deletePermissionAPI(id)
    return message
  },
  /* 发送请求添加权限的action */
  async addPermission({ commit }, permission) {
    const { message } = await addPermissionAPI(permission)
    return message
  },
  /* 根据权限的ID发送请求获取权限详情的action */
  async findPermission({ commit }, id) {
    const { data } = await findPermissionAPI(id)
    return data
  },
  /* 根据权限的详情送请求修改权限的action  */
  async updatePermission({ commit }, permission) {
    const { message } = await updatePermissionAPI(permission)
    return message
  }
}
const mutations = {
  /* 更新权限列表的mutation */
  updatePermissionList(state, permissionList) {
    state.permissionList = permissionList
  }
}
const state = {
  permissionList: []
}
const getters = {}
export default {
  actions,
  mutations,
  state,
  getters,
  namespaced: true
}
